package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.TravelComment;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.util.UserContext;
import cn.wolfcode.trip.base.domain.Question;
import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.query.TravelCommendQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelCommendService;
import cn.wolfcode.trip.base.service.ITravelService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 游记资源控制器
 */
@RestController
@RequestMapping("travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;

    @Autowired
    private ITravelCommendService commendService;

    @PostMapping
    public JSONResult save(Travel travel){
        //设置作者
        travel.setAuthor(UserContext.getCurrentUser());
        travelService.save(travel);
        return new JSONResult();
    }

    @PutMapping("/{id}")
    public JSONResult update(Travel travel){
        travelService.update(travel);
        return new JSONResult();
    }

    @GetMapping("/{id}")
    public Travel getById(@PathVariable Long id){
        return travelService.getById(id);
    }

    /**
     * 查询所有游记列表
     * @param qo
     * @return
     */
    @GetMapping
    public PageInfo listTravels(TravelQuery qo){
        //修改为 发布状态
        qo.setState(Travel.STATE_RELEASE);
        //设置为 公开的
        qo.setIsPublic(true);
        //按照发布时间倒序
        qo.setOrderBy("t.releaseTime desc");
        PageInfo query = travelService.query(qo);
        return query;
    }

    /**
     * 推荐的游记
     * @param qo
     * @return
     */
    @GetMapping("commends")
    public PageInfo listCommends(TravelCommendQuery qo){
        //按照发布时间倒序
        qo.setOrderBy("schedule desc");
        return commendService.queryForApp(qo);
    }

    /**
     * 根据输入的内容来进行高级查询
     * @param qo
     * @return
     */
    @PostMapping("selecttravelbyid")
    public JSONResult saveOrUpdate(TravelQuery qo){
        PageInfo list = travelService.query(qo);
        JSONResult json = new JSONResult();
        json.setResult(list);
        return json;
    }

    // 获取某游记下评论
    @GetMapping("/{tid}/comments/")
    public PageInfo<TravelComment> getCommentsByTravelId(@PathVariable Long tid, QueryObject qo){
        //按照发布时间倒序
        qo.setOrderBy("createTime desc");
        PageInfo<TravelComment> info = travelService.getCommentsByTravelId(tid, qo);
        return info;
    }

    // 保存游记评论
    @PostMapping("/{tid}/comments/save")
    public JSONResult save(@PathVariable Long tid, TravelComment travelComment){
        Travel travel = new Travel();
        travel.setId(tid);
        travelComment.setTravel(travel);
        travelService.saveComment(travelComment);
        return new JSONResult();
    }
}
